package com.mall.shopping.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.shopping.constant.ShoppingServiceEnum;
import com.mall.shopping.dto.bo.resp.CategoriesResp;
import com.mall.shopping.dto.bo.resp.ProductDetailResp;
import com.mall.shopping.dto.entity.TbItem;
import com.mall.shopping.dto.entity.TbItemCat;
import com.mall.shopping.dto.model.ProductCateModel;
import com.mall.shopping.dto.model.ProductDetailModel;
import com.mall.shopping.mapper.TbItemMapper;
import com.mall.shopping.service.TbItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author xrj
 * @since 2022-05-01
 */
@Service
@Component
@Slf4j
public class TbItemServiceImpl extends ServiceImpl<TbItemMapper, TbItem> implements TbItemService {

    @Autowired
    private TbItemCatServiceImpl itemCatService;

    @Autowired
    private TbItemMapper itemMapper;

    @Override
    public CategoriesResp queryItemCategory() {
        CategoriesResp resp = new CategoriesResp();
        ArrayList<ProductCateModel> cateModels = new ArrayList<>();
        try {
            List<TbItemCat> itemCats = itemCatService.list();
            for (TbItemCat itemCat : itemCats) {
                ProductCateModel cateModel = new ProductCateModel();
                BeanUtils.copyProperties(itemCat,cateModel);
                cateModel.setIconUrl(itemCat.getIcon());
                cateModels.add(cateModel);
            }
            resp.setProductCateModels(cateModels);
            resp.setCode(ShoppingServiceEnum.SUCCESS.getCode());
            resp.setMsg(ShoppingServiceEnum.SUCCESS.getMsg());
        } catch (Exception e) {
            log.error("查询商品分类信息异常");
            resp.setCode(ShoppingServiceEnum.DATA_QUERY_ERROR.getCode());
            resp.setMsg(ShoppingServiceEnum.DATA_QUERY_ERROR.getMsg());
        }
        return resp;
    }

    @Override
    public ProductDetailResp queryProductDetail(Long productId) {
        ProductDetailResp resp = new ProductDetailResp();
        ProductDetailModel detailModel = null;
        try {
            detailModel = itemMapper.queryProductDetail(productId);
            String image = detailModel.getImage();
            if (StringUtils.isNotBlank(image)){
                String[] split = image.split(",");
                detailModel.setProductImageSmall(Arrays.asList(split));
                detailModel.setImage(null);
            }
        } catch (Exception e) {
            resp.setCode(ShoppingServiceEnum.DATA_QUERY_ERROR.getCode());
            resp.setMsg(ShoppingServiceEnum.DATA_QUERY_ERROR.getMsg());
        }
        resp.setDetailModel(detailModel);
        resp.setCode(ShoppingServiceEnum.SUCCESS.getCode());
        resp.setMsg(ShoppingServiceEnum.SUCCESS.getMsg());
        return resp;
    }
}
